package pre.lxm.demo.chart.horizontal;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.Rect;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import pre.lxm.demo.chart.bean.RecordBean;

/**
 * 八方情报战绩走势图数据View
 */
public class NoScrollRecordChartView extends View {
    private int mWidth, mHeight;
    private int itemHeight;
    private int itemWidth;

    private Paint mPaint;
    private TextPaint mTextPaint;

    private List<RecordBean> mRecordList;
    private int lineHeight;
    private int leftPadding, rightPadding;
    private static final String host = "主";
    private static final String guest = "客";
    private static int defaultNum = 8;


    public NoScrollRecordChartView(Context context) {
        this(context, null);
    }

    public NoScrollRecordChartView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NoScrollRecordChartView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        itemWidth = getScreenWidth(getContext()) / defaultNum;
        lineHeight = dip2px(1);
        leftPadding = rightPadding = dip2px(16);
        initPaint();
        initTextPaint();
        mRecordList = new ArrayList<>();
    }

    /**
     * 初始化画笔
     */
    private void initPaint() {
        mPaint = new Paint();
        mPaint.setColor(Color.parseColor("#E1E1E1"));
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(lineHeight);
    }

    /**
     * 初始化文字画笔
     */
    private void initTextPaint() {
        mTextPaint = new TextPaint();
        mTextPaint.setTextSize(sp2px(14));
        mTextPaint.setStrokeWidth(2);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int num = mRecordList.isEmpty() ? (defaultNum - 2) : (mRecordList.size());
        mWidth = leftPadding + rightPadding + itemWidth * num;
        mHeight = getMeasuredHeight();
        itemHeight = (mHeight - lineHeight * 2) / 4;
        setMeasuredDimension(mWidth, getMeasuredHeight());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawCore(canvas);
        drawRecordData(canvas);
    }

    public void refreshData(List<RecordBean> recordList) {
        mRecordList.clear();
        mRecordList.addAll(recordList);
        requestLayout();
        invalidate();
    }

    private void drawCore(Canvas canvas) {

        // 绘制横线
        mPaint.setStrokeWidth(lineHeight);
        mPaint.setColor(Color.parseColor("#E1E1E1"));
        canvas.drawLine(0, lineHeight, mWidth, lineHeight, mPaint);
        canvas.drawLine(0, lineHeight + itemHeight, mWidth, lineHeight + itemHeight, mPaint);

        // 轮次栏底色
        Rect turnRect = new Rect(0, lineHeight, mWidth, lineHeight + itemHeight);
        mPaint.setColor(Color.parseColor("#F5F5F5"));
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawRect(turnRect, mPaint);

//        // 轮次
//        canvas.save();
//        // canvas.translate(leftPadding, 0);
//        mTextPaint.setColor(Color.parseColor("#888888"));
//        drawText(canvas, mTextPaint, "轮次", leftPadding * 2 + itemWidth, itemHeight);
//        canvas.restore();

        // 胜平负底色
        canvas.save();
        canvas.translate(0, itemHeight + lineHeight * 2);
        Rect resultRect = new Rect(0, 0, mWidth, mHeight);
        mPaint.setColor(Color.parseColor("#FFFFFF"));
        canvas.drawRect(resultRect, mPaint);
        canvas.restore();

//        // 胜平负
//        canvas.save();
//        mTextPaint.setColor(Color.BLACK);
//        for (int i = 0; i < resultStr.length; i++) {
//            canvas.translate(0, itemHeight + lineHeight * 2);
//            drawText(canvas, mTextPaint, resultStr[i], leftPadding + itemWidth, itemHeight);
//        }
//        canvas.restore();

        // 虚线
        canvas.save();
        mPaint.setStrokeWidth(lineHeight);
        canvas.translate(leftPadding, itemHeight + lineHeight * 2);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.parseColor("#CECECE"));
        mPaint.setAntiAlias(true);
        PathEffect pathEffect = new DashPathEffect(new float[]{10, 10, 10, 10}, 1);
        for (int i = 0; i < 3; i++) {
            mPaint.setPathEffect(pathEffect);
            Path path = new Path();
            path.moveTo(0, itemHeight / 2);
            path.lineTo(mRecordList.isEmpty() ? (mWidth - itemWidth) : (mRecordList.size() * itemWidth), itemHeight /
                    2);
            canvas.drawPath(path, mPaint);
            canvas.translate(0, itemHeight);
        }
        canvas.restore();

    }

    private void drawRecordData(Canvas canvas) {

        // 轮次数据
        mTextPaint.setColor(Color.parseColor("#888888"));
        canvas.save();
        canvas.translate(leftPadding, 0);
        for (int i = 0; i < mRecordList.size(); i++) {
//            Rect bounds = new Rect(0, 0, itemWidth, itemHeight);
//            canvas.drawRect(bounds, mPaint);
            drawText(canvas, mTextPaint, mRecordList.get(i).getTurn(), itemWidth, itemHeight);
            canvas.translate(itemWidth, 0);
        }
        canvas.restore();

        // 实线
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(false);
        // 客、主
        mTextPaint.setColor(Color.parseColor("#FFFFFF"));

        canvas.save();
        canvas.translate(leftPadding, itemHeight + lineHeight * 2);
        for (int i = 0; i < mRecordList.size(); i++) {
            RecordBean curRecord = mRecordList.get(i);
            int curYPoint = 0;
            switch (curRecord.getResult()) {
                case "3":// 胜
                    curYPoint = itemHeight / 2;
                    break;
                case "1":// 平
                    curYPoint = itemHeight * 3 / 2;
                    break;
                case "0":// 负
                    curYPoint = itemHeight * 5 / 2;
                    break;
            }
            if (i < mRecordList.size() - 1) {
                int nextYPoint = 0;
                RecordBean nextRecord = mRecordList.get(i + 1);
                switch (nextRecord.getResult()) {
                    case "3":// 胜
                        nextYPoint = itemHeight / 2;
                        break;
                    case "1":// 平
                        nextYPoint = itemHeight * 3 / 2;
                        break;
                    case "0":// 负
                        nextYPoint = itemHeight * 5 / 2;
                        break;
                }
                mPaint.setColor(Color.parseColor("#222222"));
                canvas.drawLine(itemWidth / 2 + itemWidth * i, curYPoint, itemWidth * 3 / 2 + itemWidth * i, nextYPoint,
                        mPaint);
            }

            String isHost = curRecord.getIsHost();
            if (isHost.equals("g")) {
                //客 方形
                Rect mRect = new Rect();
                mTextPaint.getTextBounds(host, 0, host.length(), mRect);

                mPaint.setColor(Color.parseColor("#FF8500"));
                Rect bounds = new Rect(itemWidth * i, curYPoint - itemHeight / 2, itemWidth + itemWidth * i,
                        itemHeight + curYPoint - itemHeight / 2);
                bounds.inset(mRect.width(), mRect.height());
                Rect hostRect = new Rect(bounds.centerX() - mRect.width(),
                        bounds.centerY() - mRect.height(),
                        bounds.centerX() + mRect.width(),
                        bounds.centerY() + mRect.height());

                canvas.drawRect(hostRect, mPaint);

                Paint.FontMetricsInt fontMetrics = mTextPaint.getFontMetricsInt();
                float baseline = itemHeight / 2 + (fontMetrics.descent - fontMetrics.ascent) / 2 - fontMetrics
                        .descent;
                canvas.drawText(guest, itemWidth / 2 + itemWidth * i, baseline + curYPoint - itemHeight / 2,
                        mTextPaint);
            } else if (isHost.equals("h")) {
                //主  圆形
                Rect mRect = new Rect();
                mTextPaint.getTextBounds(guest, 0, guest.length(), mRect);

                mPaint.setColor(Color.parseColor("#0081E6"));
                int centerX = itemWidth / 2;
                int centerY = curYPoint;
                canvas.drawCircle(itemWidth * i + centerX, curYPoint, mRect.height(), mPaint);

                Paint.FontMetricsInt fontMetrics = mTextPaint.getFontMetricsInt();
                float baseline = itemHeight / 2 + (fontMetrics.descent - fontMetrics.ascent) / 2 - fontMetrics
                        .descent;
                canvas.drawText(host, itemWidth / 2 + itemWidth * i, baseline + curYPoint - itemHeight / 2,
                        mTextPaint);
            }
        }
        canvas.restore();
    }

    private Rect drawText(Canvas canvas, TextPaint textPaint, String txtString, int txtMeasureWidth,
                          int txtMeasureHeight) {
        Rect mRect = new Rect();
        textPaint.getTextBounds(txtString, 0, txtString.length(), mRect);
        Paint.FontMetricsInt fontMetrics = textPaint.getFontMetricsInt();
        float baseline = txtMeasureHeight / 2 + (fontMetrics.descent - fontMetrics.ascent) / 2 - fontMetrics.descent;
        canvas.drawText(txtString, txtMeasureWidth / 2, baseline, textPaint);
        return mRect;
    }

    private int sp2px(float spValue) {
        final float fontScale = getContext().getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    private int dip2px(float dipValue) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    private static int getScreenWidth(Context context) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        return dm.widthPixels;
    }
}
